/*
 * Copyright (C), 2002-2018, 上海巧房信息技术有限公司
 * FileName: PremisesDynamicApiServiceImpl.java
 * Author:   liushihao
 * Date:     2018年6月13日 下午2:50:49
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>;    <time>     <version>   <desc>
 * 修改人姓名     修改时间    版本号       描述
 */
package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.PremisesDynamicApiService;
import com.yixun.qfbao.apidto.GetPremisesDynamicReqDto;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.PremisesDynamic;
import com.yixun.qfbao.xf.model.dto.PremisesDynamicDto;
import com.yixun.qfbao.xf.model.dto.PremisesDynamicResultDto;
import com.yixun.qfbao.xf.service.PremisesDynamicService;
import com.yixun.qfbao.xf.service.PremisesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: <br/>
 * 〈功能详细描述〉
 *
 * @author liushihao
 * @see [相关类/方法](可选)
 * @since [产品/模块版本](可选)
 */
@Service("premisesDynamicApiService")
public class PremisesDynamicApiServiceImpl implements PremisesDynamicApiService {

	@Autowired
	private PremisesDynamicService premisesDynamicService;

	@Autowired
	private PremisesService premisesService;

	@Override
	public List<PremisesDynamicResultDto> getPremisesDynamicList(GetPremisesDynamicReqDto reqDto) {
		List<PremisesDynamicResultDto> data = new ArrayList<>();
		PremisesDynamicDto entity = new PremisesDynamicDto();
		entity.setPremisesId(reqDto.getPremisesId());
		String orderSql = " update_time desc ";
		if(!reqDto.isListFlag()) {
		    orderSql += "limit 1";
        }
        entity.setOrders(orderSql);
		List<PremisesDynamic> list = premisesDynamicService.selectListByDto(entity);
		if (list != null && !list.isEmpty()) {
			list.forEach(item -> {
				if(item.getCanSee() != null && Boolean.TRUE.equals(item.getCanSee())){
					PremisesDynamicResultDto result = new PremisesDynamicResultDto();
					BeanUtils.copyProperties(item, result);
					data.add(result);
				}
			});
		}
		PremisesDynamicResultDto addResult = getPremisesDiscount(reqDto.getPremisesId());
		if (addResult != null) {
			data.add(0, addResult);
		}

		return data;
	}

	/**
	 * 
	 * 功能描述: <br>
	 * 楼盘促销信息
	 * 
	 * @param premisesId
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public PremisesDynamicResultDto getPremisesDiscount(Long premisesId) {
		Premises premises = premisesService.selectById(premisesId);
		if (premises != null && !StringUtils.isEmpty(premises.getDiscount())) {
			PremisesDynamicResultDto resultDto = new PremisesDynamicResultDto();
			resultDto.setUpdateTime(premises.getUpdateTime());
			resultDto.setRemark(premises.getDiscount());
			return resultDto;
		}
		return null;
	}

}
